package com.example.umeng_link_plugin

import android.content.Context
import android.net.Uri
import android.util.Log
import androidx.annotation.NonNull
import com.umeng.umlink.MobclickLink
import com.umeng.umlink.UMLinkListener
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** UmengLinkPlugin */
class UmengLinkPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
    private lateinit var channel: MethodChannel
    private lateinit var ctx: Context
    private lateinit var data: Uri
    private var linkStr: HashMap<String, String> = HashMap()
    private var adapter: UMLinkListener = object : UMLinkListener {
        override fun onLink(path: String?, query_params: HashMap<String, String>?) {
            if (query_params != null && query_params.isNotEmpty()) {
                linkStr = query_params
            }
        }

        override fun onInstall(query_params: HashMap<String, String>?, path: Uri?) {
            if (path != null) {
                initHandleUMLinkURI(ctx, path)
            }
        }

        override fun onError(p0: String?) {
            Log.d("UMLink==", "onError")
        }

    }

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, "umeng_link_plugin")
        channel.setMethodCallHandler(this)
        ctx = flutterPluginBinding.applicationContext
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        if (call.method == "getPlatformVersion") {
            result.success("Android ${android.os.Build.VERSION.RELEASE}")
        } else if (call.method == "initHandleUMLinkURI") {
            initHandleUMLinkURI(ctx, data!!)
            if (call.arguments != null) {
                MobclickLink.getInstallParams(ctx, (call.arguments as Map<String, Boolean>).getValue("enablePasteboard"), adapter)
            }
        } else if (call.method == "getUMLinkGetInstallParams") {
            result.success(linkStr)
        } else if (call.method == "cleanLinkParams") {
            linkStr = HashMap()
        } else {
            result.notImplemented()
        }
    }

    // 注册友盟Link监听
    fun initHandleUMLinkURI(ctx: Context, data: Uri) {
        if (data != null) {
            MobclickLink.handleUMLinkURI(ctx, data, adapter)
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        data = binding.activity?.intent?.data ?: Uri.parse("")
    }

    override fun onDetachedFromActivityForConfigChanges() {
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
        data = binding.activity?.intent?.data ?: Uri.parse("")
    }

    override fun onDetachedFromActivity() {
    }
}


